package jsp;

import exception.EncodingErrorException;
import util.CookieUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

/**
 * @author MissChen
 * @date 2024年05月14日 15:51
 */

/**
 * 当用户登录的时候判断用户输入的用户名与密码是否与数据库中数据一致
 * 如果一致
 *      表示登录成功，将正确的username和password保存到cookie中去
 *      且页面跳转到main.jsp
*  如果不一致
 *      则说明输入的用户名或者密码错误
*           页面仍然停留在login.jsp
 *          且在页面中展示错误消息
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username);
        System.out.println(password);
        System.out.println("==============");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String url = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, "root", "root");
            String sql=new StringBuffer()
                    .append(" select id,username,password ")
                    .append(" from t_user ")
                    .append(" where username=? ")
                    .append(" and password=? ")
                    .toString();
            ps=conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            rs=ps.executeQuery();
            if(rs.next()){

                //登录成功后将当前用户名和密码保存到cooke中去
                CookieUtil.addCookie("username",username,86400,response);
                CookieUtil.addCookie("password",password,86400,response);
                //页面直接跳转到主页
                response.sendRedirect(request.getContextPath()+"/jsp/main.jsp");
                return;
            }
            //登录失败转发到登录页面进行登录

            request.setAttribute("msg","用户名或者密码错误");
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","服务器内部异常");
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        }  finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
